package com.seh.contentcenter.controller;

import com.seh.contentcenter.domain.Result;
import com.seh.contentcenter.dto.PostDTO;
import com.seh.contentcenter.entity.Post;
import com.seh.contentcenter.service.PostService;
import com.seh.contentcenter.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    private PostService postService;

    /**
     * 查询所有帖子
     *
     * @return
     */
    @GetMapping("/findPost/{pageIndex}")
    public Result<List<PostDTO>> getPostList(@PathVariable Integer pageIndex) {
        return ResultUtil.postInfo(postService.getPostList(pageIndex));
    }

    /**
     * 根据帖子id查询单一帖子
     *
     * @param postId
     * @return
     */
    @GetMapping("/findPostById/{postId}")
    public Result<PostDTO> getPostById(@PathVariable Integer postId) {
        return ResultUtil.postInfo(postService.selectPostById(postId));
    }

    /**
     * 根据用户ID查询该用户帖子
     *
     * @param userId
     * @return
     */
    @GetMapping("/findPostByUserId")
    public Result<List<PostDTO>> getPostByUserId(@RequestParam("userId") Integer userId,
                                                 @RequestParam("pageIndex") Integer pageIndex) {
        return ResultUtil.postInfo(postService.getPostByUserId(userId, pageIndex));
    }

    /**
     * 逻辑删除帖子，根据帖子的主键ID删除帖子
     *
     * @param postId
     * @return
     */
    @GetMapping("/deletePostById/{postId}")
    public String deletePostById(@PathVariable Integer postId) {
        return postService.deletePostById(postId);
    }
}
